package com.jusfoun.communication.controller;

import com.jusfoun.communication.model.AppInfo;
import com.jusfoun.communication.service.ManageService;
import com.jusfoun.communication.util.PageInfo;
import com.jusfoun.communication.util.Result;
import com.jusfoun.communication.util.ResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 系统管理接口,查询app列表,新增app
 *
 * @author player six
 * @email jslcylcy@163.com
 * @create 2017-10-12 10:56
 **/
@RestController
@RequestMapping("/manage")
@ApiIgnore
public class ManageController {
    private static final Logger logger = LoggerFactory.getLogger(ManageController.class);

    @Autowired
    private ManageService manageService;

    @Autowired
    private ResultUtil resultUtil;

    /**
     * 获取app 列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/apps", method = RequestMethod.GET)
    public PageInfo<AppInfo> list(@RequestParam(value = "pageNum", required = false) Integer pageNum, @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        logger.info("获取app列表");
        return manageService.list(pageNum, pageSize);
    }


    /**
     * 新增app,返回
     *
     * @return
     */
    @RequestMapping(value = "/app-add", method = RequestMethod.POST)
    public Result add(AppInfo appInfo) {
        logger.info("新增app");
        if (StringUtils.isBlank(appInfo.getName())) {
            logger.info("缺少参数");
            return resultUtil.fail("失败", "app名称不能为空");
        }
        return manageService.addApp(appInfo);
    }

    /**
     * 重新生成appSecret
     *
     * @return
     */
    @RequestMapping(value = "/generate-secret", method = RequestMethod.POST)
    public Result generateSecret(AppInfo appInfo) {
        logger.info("刷新appSecret:" + appInfo.getAppId());
        if (StringUtils.isBlank(appInfo.getAppId())) {
            logger.info("缺少参数");
            return resultUtil.fail("失败", "appId不能为空");
        }
        return manageService.generateSecret(appInfo);
    }


}
